From 74961d4dfb394c098da494f6beacdd994c089566 Mon Sep 17 00:00:00 2001 From: boludoz Date: Sun, 15 Oct 2023 21:40:10 -0300 Subject: Less code, simpler, better. --- src/common/fs/fs_util.cpp | 59 ----------------------------------------------- src/common/fs/fs_util.h | 22 +----------------- src/yuzu/main.cpp | 16 +++++++++++-- 3 files changed, 15 insertions(+), 82 deletions(-) diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index e77958224..813a713c3 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp @@ -36,63 +36,4 @@ std::string PathToUTF8String(const std::filesystem::path& path) { return ToUTF8String(path.u8string()); } -std::u8string U8FilenameSanitizer(const std::u8string_view u8filename) { - std::u8string u8path_sanitized{u8filename.begin(), u8filename.end()}; - size_t eSizeSanitized = u8path_sanitized.size(); - - // The name is improved to make it look more beautiful and prohibited characters and shapes are - // removed. Switch is used since it is better with many conditions. - for (size_t i = 0; i < eSizeSanitized; i++) { - switch (u8path_sanitized[i]) { - case u8':': - if (i == 0 || i == eSizeSanitized - 1) { - u8path_sanitized.replace(i, 1, u8"_"); - } else if (u8path_sanitized[i - 1] == u8' ') { - u8path_sanitized.replace(i, 1, u8"-"); - } else { - u8path_sanitized.replace(i, 1, u8" -"); - eSizeSanitized++; - } - break; - case u8'\\': - case u8'/': - case u8'*': - case u8'?': - case u8'\"': - case u8'<': - case u8'>': - case u8'|': - case u8'\0': - u8path_sanitized.replace(i, 1, u8"_"); - break; - default: - break; - } - } - - // Delete duplicated spaces and dots - for (size_t i = 0; i < eSizeSanitized - 1; i++) { - if ((u8path_sanitized[i] == u8' ' && u8path_sanitized[i + 1] == u8' ') || - (u8path_sanitized[i] == u8'.' && u8path_sanitized[i + 1] == u8'.')) { - u8path_sanitized.erase(i, 1); - i--; - } - } - - // Delete all spaces and dots at the end of the name - while (u8path_sanitized.back() == u8' ' || u8path_sanitized.back() == u8'.') { - u8path_sanitized.pop_back(); - } - - if (u8path_sanitized.empty()) { - return u8""; - } - - return u8path_sanitized; -} - -std::string UTF8FilenameSanitizer(const std::string_view filename) { - return ToUTF8String(U8FilenameSanitizer(ToU8String(filename))); -} - } // namespace Common::FS diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index daec1f8cb..2492a9f94 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h @@ -82,24 +82,4 @@ concept IsChar = std::same_as; */ [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); -/** - * Fix filename (remove invalid characters) - * - * @param u8_string dirty encoded filename string - * - * @returns utf8_string sanitized filename string - * - */ -[[nodiscard]] std::u8string U8FilenameSanitizer(const std::u8string_view u8filename); - -/** - * Fix filename (remove invalid characters) - * - * @param utf8_string dirty encoded filename string - * - * @returns utf8_string sanitized filename string - * - */ -[[nodiscard]] std::string UTF8FilenameSanitizer(const std::string_view filename); - -} // namespace Common::FS \ No newline at end of file +} // namespace Common::FS diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index c3c12eeec..f2b91a7f3 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -2849,9 +2849,21 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path, bool shortcut_succeeded = false; + // Copy characters if they are not illegal in Windows filenames + std::string filename = ""; + const std::string illegal_chars = "<>:\"/\\|?*"; + filename.reserve(name.size()); + std::copy_if(name.begin(), name.end(), std::back_inserter(filename), + [&illegal_chars](char c) { return illegal_chars.find(c) == std::string::npos; }); + + if (filename.empty()) { + LOG_ERROR(Frontend, "Filename is empty"); + shortcut_succeeded = false; + return shortcut_succeeded; + } + // Replace characters that are illegal in Windows filenames - std::filesystem::path shortcut_path_full = - shortcut_path / Common::FS::UTF8FilenameSanitizer(name); + std::filesystem::path shortcut_path_full = shortcut_path / filename; #if defined(__linux__) || defined(__FreeBSD__) shortcut_path_full += ".desktop"; -- cgit v1.2.3